package com.example.demo.TakeMaterial.controller;


import com.example.demo.TakeMaterial.dto.purchaseGood;
import com.example.demo.TakeMaterial.entity.MaterialRequisition;
import com.example.demo.TakeMaterial.entity.RequisitionHeader;
import com.example.demo.TakeMaterial.service.RequisitionHeaderService;
import com.example.demo.common.ResponseData;
import com.example.demo.entryWarehouse.dto.Acceptance;
import com.example.demo.entryWarehouse.mapper.AcceptGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangxuan
 * @since 2021-05-17
 */
@RestController
@RequestMapping("/TakeMaterial/requisition-header")
@PreAuthorize("hasAnyAuthority('1','2','4','5')")
@CrossOrigin(origins = {"*"})
public class RequisitionHeaderController {

    @Autowired
    private  RequisitionHeaderService requisitionHeaderService;


    //所有的申请记录
    @GetMapping("/selectAll")
    public ResponseData selectAll1(@RequestParam(name = "department", required = false) String department){
        System.out.println(department);
        return requisitionHeaderService.selectAll1(department);
    }

    //所有的申请记录
    @GetMapping("/selectAll1")
    public ResponseData selectAll(){
        return requisitionHeaderService.selectAll();
    }

    //所有未完成的申请记录
    @GetMapping("/selectByNot")
    public ResponseData selectByNot(){
        return requisitionHeaderService.selectByNot();
    }

    //通过领用流水号查验收流水号
    @GetMapping("/selectByReId")
    public ResponseData selectByReId(@RequestParam("requisitionId") String requisitionId){
        System.out.println(requisitionId);
        return requisitionHeaderService.selectByReID(requisitionId);
    }

    //新建申请
    @PostMapping("/AddPurchase")
    public ResponseData AddPurchase(@RequestBody purchaseGood Goods){
        //System.out.println(Goods);
        return requisitionHeaderService.AddPurchase(Goods);
    }

    @GetMapping("/selectByAccId")
    public ResponseData selectByAccId(@RequestParam("acceptId") String acceptId){
        return requisitionHeaderService.selectByAccId(acceptId);
    }

    @PostMapping("/RemoveAdmin")
    public ResponseData RemoveAdmin(@RequestParam("id") String ID){
        return requisitionHeaderService.RemoveAdmin(ID);
    }

    //更改申请状态
    @PostMapping("/editStatus")
    public ResponseData editStatus(@RequestBody RequisitionHeader requisitionHeader){
        //System.out.println("edit");
        System.out.println(requisitionHeader);
        return  requisitionHeaderService.editStatus(requisitionHeader);
    }

    //审核通过
    @PostMapping("/auditPassed")
    public ResponseData auditPassed(@RequestParam("requisitionId") String requisitionId){
        System.out.println("updateStatus");
        System.out.println(requisitionId);
        return  requisitionHeaderService.auditPassed(requisitionId);
    }

    //审核通过，交给单位审核
    @PostMapping("/departPassed")
    public ResponseData departPassed(@RequestParam("requisitionId") String requisitionId){
        System.out.println("updateStatus");
        System.out.println(requisitionId);
        return  requisitionHeaderService.departPassed(requisitionId);
    }

    //审核失败
    @PostMapping("/auditFailed")
    public ResponseData auditFailed(@RequestParam("requisitionId") String requisitionId){
        System.out.println("auditFailed");
        System.out.println(requisitionId);
        return  requisitionHeaderService.auditFailed(requisitionId);
    }

    //审核二通过
    @PostMapping("/unitPassed")
    public ResponseData unitPassed(@RequestParam("requisitionId") String requisitionId){
        System.out.println("updateStatus");
        System.out.println(requisitionId);
        return  requisitionHeaderService.unitPassed(requisitionId);
    }

    //审核二失败
    @PostMapping("/unitFailed")
    public ResponseData unitFailed(@RequestParam("requisitionId") String requisitionId){
        System.out.println("auditFailed");
        System.out.println(requisitionId);
        return  requisitionHeaderService.unitFailed(requisitionId);
    }


    // 根据状态查询
    @GetMapping(value = "selectByStatus", produces = {"application/json;charset=UTF-8"})
    public ResponseData selectByStatus(@RequestParam("status") String status) {
        return requisitionHeaderService.selectByStatus(status);
    }

    // 处理出库退回请求
    // 出库退回按删除处理
    @PostMapping("/deliverCancel")
    public ResponseData cancel(@RequestParam("requisitionId") String requisitionId) {
//        return requisitionHeaderService.updateStatus(requisitionId, "退回");
        return requisitionHeaderService.deleteByRequisitionId(requisitionId);
    }
}

